package com.example.cinema.toolclass;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

//运行环境检测
public class AmbientInspect {
    private Context context;
    public int result = 0;

    public AmbientInspect(Context context) {
        this.context = context;
        Sensor();
        Cpu();
        CheckPipes();
        Camera();
        CameraFlash();
    }

    //判断光感器
    private void Sensor() {
        SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_LIGHT);
        int sensize = manager.getSensorList(Sensor.TYPE_ALL).size();
        Log.i("OOOO", sensize + "");
        if (sensor != null && sensize > 20) {
            result++;
        }
    }

    //判断CPU
    private void Cpu() {
        String results = null;
        try {
            String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
            ProcessBuilder pb = new ProcessBuilder(args);
            Process pc = null;

            pc = pb.start();

            StringBuffer sb = new StringBuffer();
            String readline = "";
            BufferedReader reader = new BufferedReader(new InputStreamReader(pc.getInputStream(), "utf-8"));
            while ((readline = reader.readLine()) != null) {
                sb.append(readline);
            }
            reader.close();

            results = sb.toString().toLowerCase();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (results.contains("intel") || !results.contains("amd")) {
            result++;
        }
    }

    //检测模拟器特有文件
    private void CheckPipes() {
        String[] pipes = {"/dev/socket/qemud", "/dev/qemu_pipe"};
        for (String st : pipes) {
            File pf = new File(st);
            if (pf.exists()) {
                result--;
            } else {
                result++;
            }
        }
    }

    //相机判断
    private void Camera() {
        boolean b = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
        if (b) {
            result++;
        }
    }

    //闪光灯判断
    private void CameraFlash() {
        boolean b = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        if (b) {
            result++;
        }
    }

    /***********************************/
    public String GetImei()
    {
        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
           return   manager.getImei();
        }else {
            return  manager.getDeviceId();
        }
    }



}
